/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 * 注意：
 * 本软件为www.tonetn.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.xunyin.modules.commontokentemp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.common.utils.QueryHelpPlus;
import com.xunyin.modules.commontokentemp.domain.TCommonTokenTemp;
import com.xunyin.modules.commontokentemp.service.TCommonTokenTempService;
import com.xunyin.modules.commontokentemp.service.dto.TCommonTokenTempQueryCriteria;
import com.xunyin.modules.commontokentemp.service.mapper.TCommonTokenTempMapper;
import com.xunyin.modules.device.domain.TDevice;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * @author admin
 * @date 2022-10-15
 */
@Service
@RequiredArgsConstructor
@Slf4j
//@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TCommonTokenTempServiceImpl extends BaseServiceImpl<TCommonTokenTempMapper, TCommonTokenTemp> implements TCommonTokenTempService {

    @Autowired
    private TCommonTokenTempMapper tCommonTokenTempMapper;


    @Override
    //@Cacheable
    public Map<String, Object> queryAll(TCommonTokenTempQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TCommonTokenTemp> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<TCommonTokenTemp> queryAll(TCommonTokenTempQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(TDevice.class, criteria));
    }

    public TCommonTokenTemp selectByOfficialName(String appId) {
        LambdaQueryWrapper<TCommonTokenTemp> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TCommonTokenTemp::getOfficialName, appId);
        return baseMapper.selectOne(wrapper);
    }

    public void updateOfficialName(TCommonTokenTemp commonTokenTemp) {
        LambdaQueryWrapper<TCommonTokenTemp> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TCommonTokenTemp::getOfficialName, commonTokenTemp.getOfficialName());
        baseMapper.update(commonTokenTemp, wrapper);
    }


}
